﻿#include "Pacs_MyTextEdit.h"
#include <QMetaType>

Q_DECLARE_METATYPE(textValue)

Pacs_MyTextEdit::Pacs_MyTextEdit(QWidget *parent)
	: QTextEdit(parent),
	 m_appentTextCount(0)
{
	m_strListView = nullptr;
}

Pacs_MyTextEdit::~Pacs_MyTextEdit()
{
	if (m_strListView != nullptr)
		delete m_strListView;
}

void Pacs_MyTextEdit::setShowText(QString str)
{
	if (str.trimmed().length() <= 0)
	{
		return;
	}
	QString textString = this->toPlainText();
	if (textString.length() <= 0)
	{
		setEditText(str);
	} 
	else
	{
		m_appentTextCount++;
		setAppendText(str);
	}
}
void Pacs_MyTextEdit::coverShowText(QString str) 
{
	if (str.trimmed().length() <= 0)
	{
		return;
	}
	setEditText(str);
}
void Pacs_MyTextEdit::mousePressEvent(QMouseEvent *e)
{
	QTextEdit::mousePressEvent(e);
	if (e->button() == Qt::LeftButton)
	{
		if (m_strListView != nullptr)
		{
			m_strListView->hide();
			delete m_strListView;
			m_strListView = nullptr;
		}
			
		QTextCursor cursor = this->textCursor();
		QTextCharFormat format = cursor.charFormat();
		QString strName = format.foreground().color().name();
		if (cursor.hasSelection()|| strName == "#000000")
		{
			if (m_strListView != nullptr)
			{
				m_strListView->hide();
				delete m_strListView;
				m_strListView = nullptr;
			}
			return;
		}
		textValue vValue = format.property(Qt::UserRole + 2).value<textValue>();
		if (vValue.strList.length() <= 0)
		{
			if (m_strListView != nullptr) 
			{
				m_strListView->hide();
				delete m_strListView;
				m_strListView = nullptr;
			}
			return;
		}
		m_strListView = new QListWidget((QWidget*)this->parent()->parent()->parent()->parent()->parent());
		
		m_strListView->setFocusPolicy(Qt::NoFocus);
		m_strListView->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
		m_strListView->setMinimumWidth(0);
		//m_strListView->setFrameShape(QListWidget::NoFrame);
		connect(m_strListView, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(on_strView_itemClicked(QListWidgetItem*)));
		
		int maxLegth = vValue.strList[0].length();
		for (int i = 0; i < vValue.strList.length(); ++i)
		{
			QListWidgetItem *item = new QListWidgetItem(m_strListView);
			item->setText(vValue.strList[i]);
			if (vValue.strList[i].length() > maxLegth)
			{
				maxLegth = vValue.strList[i].trimmed().length();
			}
			QVariant v;
			v.setValue(vValue);
			item->setData(Qt::UserRole, v);
			m_strListView->addItem(item);
		}
		m_strListView->setMinimumHeight(44);
		m_strListView->setMinimumWidth(30);
		m_strListView->resize(maxLegth * 20, vValue.strList.length() * 22);
		m_strListView->show();
		QPoint point = e->globalPos();
		m_strListView->move(((QWidget*)this->parent()->parent()->parent()->parent()->parent())->mapFromGlobal(point));
	}
}
void Pacs_MyTextEdit::inserFormatText(int pos, QString value)
{
	int nowPos = pos + m_textAddLength;
	QTextCharFormat *charFormat = new QTextCharFormat;
	//charFormat->setObjectIndex(QTextFormat::UserObject);
	charFormat->setForeground(Qt::blue);
	QStringList list = value.split("|");
	QString currText = list[0];
	textValue tValue;
	tValue.pos = nowPos;
	tValue.strList = list;
	tValue.currentStr = currText;
	tValue.index = m_currentIndex;
	QVariant v;
	v.setValue(tValue);
	charFormat->setProperty(Qt::UserRole + 2, v);
	textValue vValue = charFormat->property(Qt::UserRole + 2).value<textValue>();
	
	QTextCursor cursor = this->textCursor();
	cursor.setPosition(nowPos);
	cursor.setCharFormat(*charFormat);
	
	cursor.insertText(list[0]);
	m_textAddLength += list[0].length();
}

void Pacs_MyTextEdit::focusIntEvent(QFocusEvent *e)
{
	if(m_strListView != nullptr)
		m_strListView->hide();
}
void Pacs_MyTextEdit::focusOutEvent(QFocusEvent *e)
{
	if (m_strListView != nullptr)
		m_strListView->hide();
}

//void Pacs_MyTextEdit:

//当前文本框为空时 则调用当前函数
void Pacs_MyTextEdit::setEditText(QString str)
{
	this->setText("");
	QTextCursor cursor = this->textCursor();
	QTextCharFormat charFormat = cursor.charFormat();
	//charFormat->setObjectIndex(QTextFormat::UserObject);
	textValue tValue;
	QVariant v;
	v.setValue(tValue);
	charFormat.setProperty(Qt::UserRole + 2, v);
	charFormat.setForeground(Qt::black);
	cursor.setCharFormat(charFormat);
	m_textAddLength = 0;
	//QMap<int, QString> list;
	QList<clicketText> list;
	int lastIndex = 0;
	for (int i = 0; i < str.length(); ++i)
	{
		int index1 = str.indexOf("[");
		if (index1 >= 0)
		{
			str = str.trimmed();
			int aaa = str.length();
			int index2 = str.indexOf("]");
			//int index3 = str.indexOf();
			//	str = str.replace(" ","");
			QString newStr = str.mid(index1, index2-index1+1);
			//newStr = newStr.replace(QString::fromLocal8Bit(" 。"), " ");
			str = str.replace(newStr, "");
			newStr = newStr.replace("[", "");
			newStr = newStr.replace("]", "");
			clicketText cValue;
			cValue.pos = index1;
			cValue.strValue = newStr;
			list.push_back(cValue);
			i = index2 - newStr.length();

		}
	}
	str += QString::fromLocal8Bit("  ");
	cursor.insertText(str);
	m_currentIndex = 0;
	for (int i = 0; i < list.length();++i)
	{
		clicketText aa = list[i];
		inserFormatText(aa.pos, aa.strValue);
		m_currentIndex++;
	}
	m_textAddLength = 0;
}
//当前文本框有字符时 则调用当前函数
void Pacs_MyTextEdit::setAppendText(QString str)
{
	QString nowText = this->toPlainText();
	int nowLength = nowText.length();
	m_textAddLength = 0;
	int index2 = str.indexOf("]");
	QList<clicketText> list;
	int lastIndex = 0;
	for (int i = 0; i < str.length(); ++i)
	{
		int index1 = str.indexOf("[");
		if (index1 >= 0)
		{
			str = str.trimmed();
			int index2 = str.indexOf("]");
			//int index3 = str.indexOf();
			//	str = str.replace(" ","");
			QString newStr = str.mid(index1, index2-index1 + 1);
		//	newStr = newStr.replace(QString::fromLocal8Bit(" 。"), " ");
			str = str.replace(newStr, "");
			newStr = newStr.replace("[", "");
			newStr = newStr.replace("]", "");
			clicketText cValue;
			cValue.pos = index1;
			cValue.strValue = newStr;
			list.push_back(cValue);
			i = index2 - newStr.length();

		}
	}
	QTextCursor cursor = this->textCursor();
	cursor.setPosition(nowLength);
	str = "\n" + str;

//	str += QString::fromLocal8Bit(" 。");
	cursor.insertText(str+"  ");
	m_currentIndex = 0;
	for (int i = 0; i < list.length(); ++i)
	{
		clicketText aa = list[i];
		inserFormatText(aa.pos +nowLength+1, aa.strValue);
		m_currentIndex++;
	}
	m_textAddLength = 0;
}

void Pacs_MyTextEdit::on_strView_itemClicked(QListWidgetItem *item)
{
	if (item == nullptr)
		return;
	textValue tValue = item->data(Qt::UserRole).value<textValue>();
	QTextCursor cursor = this->textCursor();
	QTextCharFormat format = cursor.charFormat();
	QString currentStr = tValue.currentStr;
	if (currentStr.trimmed().isEmpty()) 
	{
		return;
	}
	int pos = cursor.position();
	int currentPos = -1;
	for (int i = pos; i >= 0; --i)
	{
		QString str1 = this->toPlainText();
		QString str = str1.mid(i, currentStr.length());
		if (str == currentStr)
		{	
			currentPos = i;
			break;
		}
	}
	if (currentPos == -1) {
		return;
	}
	cursor.setPosition(currentPos);
	for (int i = 0; i < currentStr.length(); ++i)
	{
		cursor.deleteChar();
	}
	QVariant v;
	tValue.currentStr = item->text();
	v.setValue(tValue);
	format.setProperty(Qt::UserRole + 2, v);
	cursor.setCharFormat(format);
	cursor.insertText(item->text());
 	m_strListView->hide();
	
}
